<template>
  <div>
    <el-form :model="dataForm" ref="dataForm" label-width="100px" size="small" :rules="rules">
      <el-form-item label="上级渠道:" prop="parentOrganizationId">
        <organization-cascader :flag="'id'" v-model="dataForm.parentOrganizationId" :is-edit="isEdit"></organization-cascader>
      </el-form-item>
      <el-form-item label="渠道名称:" prop="organizationName">
        <el-input v-model="dataForm.organizationName" placeholder="请输入渠道名称" maxlength="20" clearable></el-input>
      </el-form-item>
      <el-form-item label="渠道类型:" prop="organizationType">
        <el-select placeholder="选择渠道类型" v-model="dataForm.organizationType">
          <el-option :value="key" :label="value" v-for="(value, key) of dicts['organization_type'].childMap" :key="key"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="渠道描述:" prop="organizationDesc">
        <el-input v-model="dataForm.organizationDesc" type="textarea" placeholder="请输入渠道描述" :rows="4" maxlength="300" show-word-limit clearable></el-input>
      </el-form-item>
      <el-form-item label="渠道状态:">
        <el-select placeholder="选择渠道状态" v-model="dataForm.organizationStatus">
          <el-option :value="key" :label="value" v-for="(value, key) of dicts['organization_status'].childMap" :key="key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-top: 10px" label-width="130px">
        <el-button type="" @click="close">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          <fa-icon class="fa-check" />提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { CONSTANT_OBJ } from '@/utils/constant'
import { common } from '@/utils/common'
import OrganizationCascader from '@/components/organizationCascader'
import api from '../api'
import { mapState } from 'vuex'
export default {
  props: { editData: Object, orgTree: Array ,isEdit: Boolean},
  components: { OrganizationCascader },
  data () {
    return {
      dataForm: {
        organizationId: '',
        parentOrganizationId: '',
        organizationName: '',
        organizationCode: '',
        organizationType: '',
        organizationStatus: 'OPEN',
        organizationDesc: '',
        principal: ''
      },
      userForm: {
        pageNum: 0,
        pageSize: 999,
        organizationId: ''
      },
      userList: [],
      principal: [],
      parentOrganizationProps: {
        checkStrictly: true,
        emitPath: false,
        value: 'organizationId',
        label: 'organizationName',
        children: 'childOrganizations'
      },
      rules: {
        parentOrganizationId: [{ required: true, message: '请选择渠道', trigger: 'blur' }],
        organizationName: [{ required: true, message: '请输入渠道名称', trigger: 'blur' }],
        organizationCode: [{ required: true, message: '请输入渠道编码', trigger: 'blur' }],
        organizationType: [{ required: true, message: '请选择渠道类型', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    if (this.editData && (this.editData.organizationId || this.editData.parentOrganizationId)) {
      this.queryOrgUser(this.editData.organizationId)
      if (this.editData.principal) {
        let ver = Object.prototype.toString
        if (ver.call(this.editData.principal) === '[object Array]') {
          this.principal = [...this.editData.principal.map(v => v.userName)]
        } else {
          this.principal = this.editData.principal.split(',')
        }
        this.queryOrgUser()
      }
      if (this.editData.revision) {
        this.dataForm.revision = this.editData.revision
      }
      common.listAssign(this.dataForm, this.editData)
    }
    console.log(this.dataForm, '新增------------------------------------')
  },
  methods: {
    queryOrgUser (id) {
      let orgId = id || this.editData.organizationId
      api.getUserOrgList([orgId]).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.userList = [...data] || []
        }
      })
    },
    submit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const dataForm = { ...this.dataForm }
          dataForm.principal = this.principal.join(',')
          if (this.editData && this.editData.organizationId) {
            this.edit(dataForm)
          } else {
            this.add(dataForm)
          }
        }
      })
    },
    // 新增
    add (dataForm) {
      api.postAddOrgData(dataForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess', 'org')
        }
      })
    },
    // 编辑
    edit (dataForm) {
      api.putEditOrgData(dataForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess', 'org')
        }
      })
    },
    close () {
      this.$emit('close')
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  }
}
</script>
